package com.baidu.littlepainter.fragments.sheet

import android.app.Dialog
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.setFragmentResult
import androidx.recyclerview.widget.LinearLayoutManager
import com.baidu.littlepainter.databinding.FragmentChooseUserSheetBinding
import com.baidu.littlepainter.databinding.FragmentPinLoginBinding
import com.baidu.littlepainter.databinding.FragmentPinRegistBinding
import com.baidu.littlepainter.fragments.base.BaseFragment
import com.baidu.littlepainter.utils.ViewUtils
import com.baidu.littlepainter.viewmodel.MainViewModel
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class ChooseUserFragment:BottomSheetDialogFragment() {
    private lateinit var binding: FragmentChooseUserSheetBinding
    private val mAdapter:UserAdapter by lazy {
        UserAdapter()
    }

    private val viewModel:MainViewModel by activityViewModels()

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = BottomSheetDialog(requireContext())
        binding = FragmentChooseUserSheetBinding.inflate(layoutInflater)
        dialog.setContentView(binding.root)
        //配置取消按钮的事件
        binding.cancelBtn.setOnClickListener {
            dismiss()
        }
        initView()
        return dialog
    }

    private fun initView(){
        binding.recyclerView.layoutManager = LinearLayoutManager(
            requireContext(),
            LinearLayoutManager.VERTICAL,
            false
        )
        binding.recyclerView.adapter = mAdapter

        //配置adapter选中的事件
        mAdapter.addSelectCallback { user ->
            val resultBundle = Bundle().apply {
                putParcelable("user", user)
            }
            setFragmentResult("chooseUser",resultBundle)
            dismiss()
        }
    }

    override fun onResume() {
        super.onResume()
        mAdapter.setUsers(viewModel.users.value!!)
    }
}